﻿/*
 * DG (DG Gaming Engine)  - API
 * Copyright (C) 2004  Bob Marks (marksie531@yahoo.com)
 * http://code.google.com/p/dng-gaming
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.dg.gaming.api.common.msg {
    import nanoxml.XMLElement;

    /**
     * Abstract base class for table messages.  This class is used for creating
     * communication objects between games e.g. CommExecuteChessMove.  Fields
     * which are most commonly used include username, status and table number.
     *
     * Note: possible future enhancement - put seat number in here and update
     * TableConnectionThread???
     *
     * @author  Bob Marks
     * @version Beta 0.3
     */
    public class MsgTableMessage extends MsgGameMessage {

        /** No table being used in the message. */
        public static const NO_TABLE:String = "-1";

        /** Table number. */
        protected var tableNum:String;

        /** XML attribute for table number. */
        public static const XML_ATT_TABLE_NUM:String = "table";



        /**
         * Creates a communcation table object with no status.  Note: the table
         * number must be set using the setTable () method.
         */
        public override function init():void {
            super.init();

            setTableNum(NO_TABLE);
        }

        /**
         * Creates a table message with a status.  Note: the table
         * number must be set using the setTable () method.
         *
         * @param status    Integer status code
         */
        public override function initStatus(status:int):void {
            super.initStatus(status);

            setTableNum(NO_TABLE);
        }

        /**
         * Creates a table message with a username.  Note: the table
         * number must be set using the setTable () method.
         *
         * @param status    Integer status code
         */
        public override function initUserId(userid:Number):void {
            super.initUserId(userid);

            setTableNum(NO_TABLE);
        }

        /**
         * Create a table message to go to a particular user.  Note: the table
         * number must be set using the setTable () method.
         *
         * @param status      Integer status code
         * @param username    Username
         */
        public override function initFull(status:int, userid:Number):void {
            super.initFull(status, userid);


        }

        /**
         * Constructor which parses the XMLElement into class fields.
         *
         * @param message  Element to parse.
         */
        public function MsgTableMessage(message:XMLElement = null) {

            setTableNum(NO_TABLE);
            // read table numbers

            if (message != null) {
                super(message); // super class reads username and status
                this.tableNum = message.getStringAttribute(XML_ATT_TABLE_NUM, NO_TABLE);
            }

        }

        /**
         * Set the table number - this is done in the API.
         *
         * @param tableNum Set the table number.
         */
        public function setTableNum(tableNum:String):void {
            this.tableNum = tableNum;
        }

        /**
         * Return the table number for this table.
         *
         * @return  Table number.
         */
        public function getTableNum():String {
            return tableNum;
        }

        /**
         * Create an abstract XMLElement which sub classes can use. Note: this
         * flatten (String name) method isn't part of the ITransmittable interface
         * but is in here for convenience.
         *
         * @param nameName of the commuication object.
         * @return          Flat XML version of this object.
         */
        public override function flattenWithName(name:String):XMLElement {
            // retrieve abstract element from super class
            var message:XMLElement = super.flattenWithName(name);

            if (tableNum != NO_TABLE) {
                message.setAttribute(XML_ATT_TABLE_NUM, tableNum);
            }

            return message; // return element back to user
        }

        /* INTERFACE com.dg.gaming.api.common.msg.ITransmittable */

        public override function flatten():XMLElement {
            return null;
        }
    }
}